//
//  ListView.m
//  IYoker_Organization
//
//  Created by chunjian Dai on 15/12/24.
//  Copyright © 2015年 wangguimin. All rights reserved.
//

#import "ListView.h"
#import "ListViewTableViewCell.h"


#define RowHeight h(150)
#define MaxRows 6

@interface ListView ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>

@property (nonatomic,strong) CALayer *tableViewLayer;

@end
@implementation ListView

- (void)dealloc{
    
    [self removeObserver:self];
    
}

- (CGFloat)rowHeight{
    if (!_rowHeight) {
        self.rowHeight = RowHeight;
    }
    return _rowHeight;
}
- (UIColor *)bgColor{
    if (!_bgColor) {
        self.bgColor = [UIColor clearColor];
    }
    return _bgColor;
}
- (CGFloat)bgAlpha{
    if (!_bgAlpha) {
        self.bgAlpha = 0.5;
    }
    return _bgAlpha;
}
- (CGFloat)y{
    if (!_y) {
        self.y = 0;
    }
    return _y;
}

// 单例方法
+ (id)defaultViewWithOffset_Y:(CGFloat)y{
    static ListView *view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[self alloc] initWithOffset_Y:y];
    });
    return view;
}

+ (id)defaultView{
    static ListView *view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[self alloc] init];
    });
    return view;
}

- (instancetype)init{
    self = [super initWithFrame:CGRectZero];
    if (self) {
        [self initView];
    }
    return self;
}
- (instancetype)initWithOffset_Y:(CGFloat)y{
    self = [super initWithFrame:CGRectMake(0, y, kScreenWidth, kScreenHeight - y)];
    if (self) {
        [self initView];
    }
    return self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (void)reloadData{
    
    CGRect frame = self.tableView.frame;
    if (self.dataSource.count > MaxRows) {
        frame.size.height = self.rowHeight * MaxRows;
    } else {
        frame.size.height = self.rowHeight * self.dataSource.count;
    }
    frame.size.width = self.width > 0 ? self.width : kScreenWidth;
    frame.origin.x = self.x > 0 ? self.x : 0;
    frame.origin.y = self.y > 0 ? self.y : 0;
    
    self.frame = frame;
    self.tableView.frame = self.bounds;
    self.tableViewLayer.frame = self.tableView.frame;


    [self.tableView reloadData];
    
}
- (void)reloadDataWithOffset_X:(CGFloat)x offset_y:(CGFloat)y width:(CGFloat)width dataSource:(NSMutableArray *)dataSource key:(NSString *)key{
    self.key = key;
    self.x = x;
    self.y = y;
    self.width = width ? width : self.width;
    self.dataSource = dataSource ? dataSource : self.dataSource;
    [self reloadData];
}
- (void)handleTapAction:(UITapGestureRecognizer *)sender{
    [self.dataSource removeAllObjects];
    [self reloadDataWithOffset_X:self.x offset_y:self.y width:self.width dataSource:self.dataSource key:@""];
    [self.tableView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(dismissListView:)]) {
        [self.delegate dismissListView:self];
    }
    [self removeFromSuperview];
}
- (void)dissmiss{
    [self handleTapAction:nil];
}
- (void)initView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.layer.masksToBounds = YES;
    self.tableView.layer.cornerRadius = kWidth(10);
    [self.tableView registerClass:[ListViewTableViewCell class] forCellReuseIdentifier:@"List"];
    self.tableView.mj_footer = self.footer;
    
    
    self.tableViewLayer = [self addShadowWithView:self.tableView shadowColor:[UIColor lightGrayColor] shadowOffset:CGSizeMake(0, 0) shadowOpacity:0.5];
    self.tableViewLayer.frame = self.tableView.frame;
    [self.layer addSublayer:self.tableViewLayer];
    
    [self addSubview:self.tableView];
    

}
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(listView:                                                     index:)]) {
        [self.delegate listView:self index:indexPath.row];
    }
    [self handleTapAction:nil];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat
{
    return self.rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ListViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"List" forIndexPath:indexPath];
    
    NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc] initWithString:self.dataSource[indexPath.row]];
    [aStr addAttribute:NSForegroundColorAttributeName  //文字颜色
                                   value:ColorWithHexString(@"50cbff")
                                   range:[self.dataSource[indexPath.row] rangeOfString:self.key]];
    
    cell.label.attributedText = aStr;

    if (indexPath.row != self.dataSource.count - 1) {
        [cell.contentView addLineWithLineWidth:h(5) startPoint:CGPointMake(kWidth(30), cell.height - h(5)) endPoint:CGPointMake(cell.contentView.width - kWidth(30), cell.height - h(5)) lineColor:ColorWithHexString(@"f0f0f0")];

    }
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (void)layoutSubviews{
    [super layoutSubviews];
}

#pragma mark 下拉刷新和上拉加载


- (MJRefreshBackNormalFooter *)footer{
    if (!_footer) {
        self.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handleFooterRefreshingAction)];
        //  设置刷新的属性
    }
    return _footer;
}

- (void)handleFooterRefreshingAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(footerActionWithListView:)]) {
        [self.delegate footerActionWithListView:self];
    }
}

- (void)endRefresh
{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
@end
